package com.baibei.module;

import android.app.Activity;
import android.app.Dialog;
import android.util.TypedValue;
import android.webkit.JavascriptInterface;

import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.model.Area;
import com.baibei.model.JsInteractedInfo;
import com.baibei.model.RankingInfo;
import com.baibei.model.UserInfo;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.RxObservable;
import com.baibei.ui.AppUI;
import com.baibei.widget.dialog.ShareDialog;
import com.blankj.utilcode.utils.LogUtils;
import com.google.gson.Gson;
import com.rae.swift.Rx;
import com.rae.swift.session.SessionManager;
import com.umeng.socialize.utils.Log;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.List;

/**
 * js桥接对象
 * Created by shenminjie on 2017/12/13.
 * mail:shenminjie@100bei.com
 */

public class EasyJsBridge {

    /**
     * 活动
     */
    private Activity mActivity;
    private Gson mGson;

    public EasyJsBridge(Activity activity) {
        this.mActivity = activity;

        mGson = new Gson();
    }


    /**
     * 获取用户信息
     */

    @JavascriptInterface
    public String getUserInfo(){
        TokenInfo userToken = SessionManager.getDefault().getUserToken();
        String accessToken = userToken.getAccessToken();
        Log.i("h5hh",accessToken);
        return accessToken;
    }

    @JavascriptInterface
    public int getUserId(){
        UserInfo userInfo = SessionManager.getDefault().getUser();
        return userInfo.getUserId();
    }


    /**
     * 返回首页
     */
    @JavascriptInterface
    public void backHome(){
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppRouter.routeToMain(mActivity,0);
            }
        });
    }
    /**
     * 去分享
     *
     */
    @JavascriptInterface
    public void toShare(){
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppRouter.routeToMain(mActivity,"share");
            }
        });

    }

    /**
     * 去卡包
     */
    @JavascriptInterface
    public void toQuan(){
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppRouter.routeToTicket(mActivity);
            }
        });
    }

    /**
     * 去登录
     */
    @JavascriptInterface
    public void toLogin(){
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppRouter.routeToLogin(mActivity);
            }
        });
    }

    /**
     * 是否登录
     *
     * @return return
     */
    @JavascriptInterface
    public boolean isLogin() {
        return SessionManager.getDefault().isLogin();
    }

    @JavascriptInterface
    public void toChongZhi(){
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppRouter.routeToRecharge(mActivity);
            }
        });
    }

    /**
     * 跳去登录操作
     */
    @JavascriptInterface
    public void login() {
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppRouter.routeToLoginJustReturn(mActivity);
            }
        });
    }

    /**
     * 获取用户信息
     * 备注，永远获取最新的数据
     *
     * @return return
     */
//    @JavascriptInterface
//    public String getUserInfo() {
//        JsInteractedInfo info = JsInteractedFactory.getJsInteractedInfo(mActivity);
//        LogUtils.e("fragment","userinfo");
//        return mGson.toJson(info);
//    }

    /**
     * 分享操作
     *
     * @param url     url
     * @param title   title
     * @param content content
     */
    @JavascriptInterface
    public void share(final String url, final String title, final String content) {
        runOnUi(new Runnable() {
            @Override
            public void run() {
                ShareDialog dialog = new ShareDialog(mActivity);
                dialog.setOnShareClickListener(new ShareDialog.OnShareClickListener() {
                    @Override
                    public void onShare(Dialog dialog) {
                        dialog.dismiss();
                    }

                    @Override
                    public void onShareFailed(Dialog dialog, String msg) {
                        dialog.dismiss();
                        AppUI.failed(mActivity, msg);
                    }
                });
                dialog.setShareWeb(url, title, content, null);
                int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, mActivity.getResources().getDisplayMetrics());
                UserInfo userInfo = SessionManager.getDefault().getUser();
                if (userInfo != null) {
                    dialog.setInvitationCode(userInfo.getInvitationCode());
                }
                dialog.show();
            }
        });
    }

    @JavascriptInterface
    public void toOrdering(final int productId){
        runOnUi(new Runnable() {
            @Override
            public void run() {
                AppUI.loading(mActivity);
                RxObservable.create(ApiFactory.getInstance().getTradeApi().getRankingList(Area.ALL.name()),"").subscribe(new ApiDefaultObserver<List<RankingInfo>>() {
                    @Override
                    protected void onError(String s) {
                        AppUI.toast(mActivity,"数据错误，跳转失败");
                    }

                    @Override
                    protected void accept(List<RankingInfo> rankingInfos) {
                        for (int i = 0 ; i < rankingInfos.size() ; i++){
                            RankingInfo rankingInfo = rankingInfos.get(i);
                            if (productId == Rx.parseInt(rankingInfos.get(i).getProductId())){
                                AppUI.dismiss();
                                AppRouter.routeToTrade(mActivity, rankingInfos.get(i).getEstimateTime(), rankingInfo, rankingInfo.getSaleTips());
                                mActivity.finish();
                                return;
                            } else if (i == rankingInfos.size()-1){
                                AppUI.toast(mActivity,"数据错误，跳转失败");
                            }
                        }
                    }
                });

            }
        });
    }


    /**
     * ui线程操作
     *
     * @param runnable runnable
     */
    private void runOnUi(Runnable runnable) {
        mActivity.runOnUiThread(runnable);
    }
}
